﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NGuice.Inject.Internal.Util
{
    /// <summary>
    /// 处理任意对象上的哈希码与Equals的工具类
    /// </summary>
    public static  class Objects
    {
        /// <summary>
        /// 判断两个对象是否Equals，参数可以为null
        /// </summary>
        public static bool Equal(object a, object b)
        {
            return a == b || (a != null && a.Equals(b));
        }

        /// <summary>
        /// 获取对象数组的哈希码
        /// </summary>
        public static int GetHashCode(params object[] objects)
        {
            return Lists.HashCode(objects);
        }

        /// <summary>
        /// 由于C#不支持？通配符形式的泛型，因此无法判断两个对象泛型定义是否相等。设计此方法的目的，是通过判断两个泛型对象的泛型定义
        /// 是否相等，从而在一定程度上支持？通配符泛型！
        /// </summary>
        /// <example>
        /// IList<string>与IList<int>在C#中是不相等，但对于需要存储通用类型的泛型来说:IList<?>应该包括这两个类型，因此他们的泛型定义式相等的！
        /// </example>
        public static bool IsGenericDefineEquals(object a, object b)
        {
            if (a == b || (a != null && a.GetType().IsGenericType))
            {
                if (!b.GetType().IsGenericType)
                    return false;
                if (a.GetType().GetGenericTypeDefinition() == b.GetType().GetGenericTypeDefinition())
                    return true;
            }
            return false;
        }

        //public static bool IsGenericDefinition(object a, Type genericType)
        //{
        //    if (a != null && a.GetType().IsGenericType && a.GetType().GetGenericTypeDefinition() == genericType.GetGenericTypeDefinition())
        //    {
        //        return true;
        //    }
        //    return false;
        //}
    }
}
